/*
 *   SlimPlayer Copyright (c) 2006 Koelec
 *
 *   This file is part of SlimPlayer plugin for SageTv.
 *
 *   Slimplayer is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   Slimplayer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with SoftSqueeze; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
package nl.cmkprojects.sage.slimplayer;

import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import sage.SageTV;

public class SageDebugAppender extends AppenderSkeleton
{
  private void logToSage(String log)
  {
    if (Boolean.getBoolean("slimplayer.standalone") == false)
    {
      try
      {
        SageTV.api("DebugLog", new Object[]
        { log });
      } catch (InvocationTargetException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    } else
    {
      System.out.println(log);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
   */
  protected void append(LoggingEvent event)
  {
    logToSage(layout.format(event));
    String throwable[] = event.getThrowableStrRep();
    if (throwable != null)
    {
      for (int i = 0; i < throwable.length; i++)
      {
        logToSage(throwable[i]);
        logToSage("\n");
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.log4j.Appender#close()
   */
  public void close()
  {
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.log4j.Appender#requiresLayout()
   */
  public boolean requiresLayout()
  {
    return true;
  }

}
